package com.lcredu.seckill.rabbitmq;

import com.lcredu.seckill.config.RabbitMQConfig;
import com.lcredu.seckill.config.RabbitMQConfigHeaders;
import com.lcredu.seckill.config.RabbitMQConfigTopic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author 黎长容
 * @date 2025-09-05 15:06
 */
@Service
@Slf4j
public class RabbitmqReceiver {
    private final Queue queue1;

    public RabbitmqReceiver(@Qualifier("queue1") Queue queue1) {
        this.queue1 = queue1;
    }

    @RabbitListener(queues = {"queue"})
    public void receiveMsg(Object msg) {
        log.info("接收到的消息为={}" + msg);
    }

    @RabbitListener(queues = {"queue_fanout01"})
    public void receiveMsg01(Object msg) {
        log.info("从 queue_fanout01 接收消息={}" + msg);
    }

    @RabbitListener(queues = {"queue_fanout02"})
    public void receiveMsg02(Object msg) {
        log.info("从 queue_fanout02 接收消息={}" + msg);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_DIRECT01)
    public void receiveDirectForSuccess(Object msg) {
        log.info("从Direct交换机中获取到了成功消息={}", msg);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_DIRECT02)
    public void receiveDirectForError(Object msg) {
        log.info("从Direct交换机中获取到了失败消息={}", msg);
    }


    //监听topic交换机绑定的队列
    @RabbitListener(queues = RabbitMQConfigTopic.QUEUE01)
    public void receiveMsgFromTopic01(Object msg) {
        log.info("从topic交换机的队列1接收到了消息={}", msg);
    }

    @RabbitListener(queues = RabbitMQConfigTopic.QUEUE02)
    public void receiveMsgFromTopic02(Object msg) {
        log.info("从topic交换机的队列2接收到了消息={}", msg);
    }

    //监听header的队列
    @RabbitListener(queues = RabbitMQConfigHeaders.QUEUE_HEADER01)
    public void receiveMsgFromHeader01(Message message) {
        log.info("从Header01的队列1接收到了消息对象={}", message);
        log.info("从Header01的队列1接收到了消息内容---->{}", new String(message.getBody()));
    }

    @RabbitListener(queues = RabbitMQConfigHeaders.QUEUE_HEADER02)
    public void receiveMsgFromHeader02(Message message) {
        log.info("从Header02的队列2接收到了消息对象={}", message);
        log.info("从Header02的队列2接收到了消息内容---->{}", new String(message.getBody()));
    }
}
